package di

import (
	"sync"

	. "gitee.com/xiawucha365/sago.v2/core/config"
	. "gitee.com/xiawucha365/sago.v2/core/log"
	. "gitee.com/xiawucha365/sago.v2/core/orm"
	"go.uber.org/dig"
)

var (
	container *DI
	diOnce    sync.Once
)

type DI struct {
	dig *dig.Container
}

func New() *DI {
	return &DI{dig: dig.New()}
}

func (di *DI) Invoke(function interface{}, opts ...dig.InvokeOption) error {
	return di.dig.Invoke(function, opts...)
}

func (di *DI) provide(constructor interface{}, opts ...dig.ProvideOption) error {
	return di.dig.Provide(constructor, opts...)
}

//===================================================================
//type Dps struct {
//	dig.In
//	fg *config.FlagOption
//	cf *config.Config
//	log *slog.Log
//	redis *orm.DbRedis
//	mysql *orm.DbMysql
//	mngodb *orm.DbMongodb
//}

func BuildContainer() *DI {
	diOnce.Do(func() {
		container = New()

		if err := container.provide(NewFlagOption); err != nil {
			panic(err)
		}
		if err := container.provide(NewConfig); err != nil {
			panic(err)
		}
		if err := container.provide(NewLog); err != nil {
			panic(err)
		}

		if err := container.provide(NewRedis); err != nil {
			panic(err)
		}
		if err := container.provide(NewDbMongo); err != nil {
			panic(err)
		}
		if err := container.provide(NewDbMysql); err != nil {
			panic(err)
		}

	})

	return container

}
